package util.ir;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;



import util.io.TextFileLoader;

public class QueryTagsModel {

	private static int _tag = 1;
	private static int _query = 0;
	private static int _freq = 2;

	HashSet<String> queries_to_eval = new HashSet<String>();

	int limit = 1;



	public QueryTagsModel(String path, HashSet<String> queries_to_eval,
			int threshold) {
		this.limit = threshold;
		this.queries_to_eval = queries_to_eval;

		try {
			initQueries(path, queries_to_eval);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// a american air www.aa.com/aa/reservation/roundTripSearchAccess.do
	// years_10_12 kids

	private Hashtable<String, ArrayList<QueryTag>> queries = new Hashtable<String, ArrayList<QueryTag>>();

	private Hashtable<String, Hashtable<String, Float>> query_hash = new Hashtable<String, Hashtable<String, Float>>();

	public Hashtable<String, ArrayList<QueryTag>> getQueries() {

		return queries;

	}

	public Hashtable<String, Hashtable<String, Float>> getQueryHash() {

		return query_hash;

	}

	public void initQueries(String in_path, HashSet<String> queries_to_eval)
			throws IOException {

		TextFileLoader in = new TextFileLoader(in_path);
		while (in.hasNext()) {

			String t[] = in.next();
			QueryTag qt = new QueryTag();

			qt.tag = t[_tag];
			qt.freq = Integer.valueOf(t[_freq]);
			String query = t[_query];

			if (!queries_to_eval.contains(query) || qt.freq < limit) {

				continue;
			}
			// System.out.println(query + "\t");
			if (queries.containsKey(query)) {

				queries.get(query).add(qt);

			} else {

				ArrayList<QueryTag> temp = new ArrayList<QueryTag>();
				temp.add(qt);
				queries.put(query, temp);
			}

			if (query_hash.containsKey(query)) {
				Hashtable<String, Float> temp = query_hash.get(query);

				if (temp.containsKey(t[_tag])) {
					Float f = temp.get(t[_tag]);
					f = f + Integer.valueOf(t[_freq]);

					temp.put(t[_tag], f);
				} else {

					temp.put(t[_tag], Float.valueOf(t[_freq]));
				}

				query_hash.put(t[_query], temp);

			} else {
				Hashtable<String, Float> temp = new Hashtable<String, Float>();
				temp.put(t[_tag], Float.valueOf(t[_freq]));
				query_hash.put(t[_query], temp);
			}
		}

	}

}
